package com.mobile.manager.controller;

import com.mobile.manager.pojo.Chart;
import com.mobile.manager.service.IChartService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by zhangdi on 16/6/7.
 */
@Controller
@RequestMapping("/device")
//public class ChartController {
//    @Resource
//    private IChartService chartService;
//    @ResponseBody
//    @RequestMapping("/showPieChart")
//    public List<Chart> getPieChartJson(HttpServletRequest request,Model model){
//        List<Chart> charts = new ArrayList<Chart>();
//
//        String os = request.getParameter("os");
//        List<HashMap> versions = this.chartService.getOSVersionByOS(os);
//        for(HashMap version : versions){
//            Chart chart = new Chart();
//            String vs=version.values().toString().replace("[", "").replace("]", "");
//            HashMap countMap = this.chartService.getCountOfVersion(vs);
//            String countString = countMap.values().toString().replace("[","").replace("]","");
//            chart.setLabels(vs);
//            chart.setData(Long.valueOf(countString));
//            charts.add(chart);
//        }
//        return charts;
//    }

public class ChartController {
    @Resource
    private IChartService chartService;
    @RequestMapping("/showPieChart")
    public String getPieChartJson(HttpServletRequest request,Model model){
//        List<Chart> charts = new ArrayList<Chart>();
//
//        String os = request.getParameter("os");
//        List<HashMap> versions = this.chartService.getOSVersionByOS(os);
//        for(HashMap version : versions){
//            Chart chart = new Chart();
//            String vs=version.values().toString().replace("[", "").replace("]", "");
//            HashMap countMap = this.chartService.getCountOfVersion(vs);
//            String countString = countMap.values().toString().replace("[","").replace("]","");
//            chart.setLabels(vs);
//            chart.setData(Long.valueOf(countString));
//            charts.add(chart);
//        }
//
//        model.addAttribute("charts", charts);
        return "showChartUI";
    }

    @RequestMapping("/chartJson")
    @ResponseBody
    public String gethartJson(){
        String data = "{'legend':['4.4.2','5.0','6.0'],'series':[4440,3220,1110]}";
        return data;
    }
    @RequestMapping("/getChartJsonOfAndroid")
    @ResponseBody
    public Chart getChartJsonOfAndroid(HttpServletRequest request,Model model){
        Chart chart = new Chart();
        ArrayList<String> legend = new ArrayList<String>();
        ArrayList<Integer> series = new ArrayList<Integer>();
        List<HashMap> versions = this.chartService.getOSVersionByOS("Android");
        for(HashMap version : versions){
            String vs=version.values().toString().replace("[", "").replace("]", "");
            legend.add(vs);

            HashMap countMap = this.chartService.getCountOfVersion(vs);
            String countString = countMap.values().toString().replace("[","").replace("]","");
            series.add(Integer.valueOf(countString));
        }
        chart.setLegend(legend);
        chart.setSeries(series);
        return chart;

    }
    @RequestMapping("/getChartJsonOfIOS")
    @ResponseBody
    public Chart getChartJsonOfIOS(HttpServletRequest request,Model model){
        Chart chart = new Chart();
        ArrayList<String> legend = new ArrayList<String>();
        ArrayList<Integer> series = new ArrayList<Integer>();
        List<HashMap> versions = this.chartService.getOSVersionByOS("IOS");
        for(HashMap version : versions){
            String vs=version.values().toString().replace("[", "").replace("]", "");
            legend.add(vs);

            HashMap countMap = this.chartService.getCountOfVersion(vs);
            String countString = countMap.values().toString().replace("[","").replace("]","");
            series.add(Integer.valueOf(countString));
        }
        chart.setLegend(legend);
        chart.setSeries(series);
        return chart;
    }

    @RequestMapping("/getBarChart")
    @ResponseBody
    public Chart getBarChart(){
        Chart chart = new Chart();
        ArrayList<String> axis = new ArrayList<String>();
        //ArrayList<Integer> serieses = new ArrayList<Integer>();
        ArrayList<Integer> series = new ArrayList<Integer>();

        ArrayList<String> legend = new ArrayList<String>();
        legend.add("设备数量");
        List<HashMap> groups = this.chartService.getAllGroup();
        for(HashMap group :groups){
            String gp=group.values().toString().replace("[", "").replace("]", "");
            axis.add(gp);

            HashMap countMap = this.chartService.getCountOfGroup(gp);
            String countString = countMap.values().toString().replace("[", "").replace("]", "");
            series.add(Integer.valueOf(countString));
        }
        chart.setSeries(series);
        chart.setAxis(axis);
        chart.setLegend(legend);
        return chart;
    }

}
